<?php

require_once './include/common.inc.php';
checkLogin();

$page_title = 'jans2en furniture - Inventory - Product Details';
$menu_id = 6;
getMyParam('categoryid');
getMyParam('inventoryid');
getMyParam('pid');
getMyParam('fid');
getMyParam('gotype');
$fid = (int)$fid;

$cate_id = $fid>0 ? $fid : $categoryid;

$sql = "SELECT `type` FROM `inventory` WHERE `id` = '$inventoryid'";
$tmp_ary = $db->get_one($sql);
$inventory_type = $tmp_ary['type'];


$sub_out_ary = array();

// =============== 1 start =============================
$sql = "SELECT n.`id`, n.`name`, n.`pic`, n.`type`, n.`update` FROM `inventory` as n, `product_inventory` as i " . $sql_where . $sql_2 . $sql_3 . " GROUP BY n.`id` ORDER BY n.`seq` ASC";

$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $inventory_show_ary[] = $rs;
    if ($inventoryid == $rs['id']) {
        $inventory_type = $rs['type'];
        $inventory_name = $rs['name'];

    }
}

if ($inventory_type == 2) {

} else {
    if (!is_numeric($categoryid) || $categoryid <=0) {
        header("Location:inventory.php");
    }
}

///************check the category's publish state 	--- leo 2008-07-02 ********************/////

$c_publish = 1;
$f_publish = 1;

if($categoryid > 1000)
	$c_publish = 1;
else
{
	if($categoryid!="" && $categoryid > 0 && is_numeric($categoryid))
	{
		$sql_publish_check = "select publish,fid from category  where id = $categoryid";
		$query = $db->query($sql_publish_check);
		$rs_check = $db->fetch_array($query);
		$c_publish = $rs_check["publish"]; 	//current category's publish state 
	}
}

if ($fid == 0 || $categoryid > 1000 )
	$f_publish = 1;
else
{
	if($fid!="" && $fid > 0 && is_numeric($fid))
	{
		$sql_publish_check = "select publish as f_publish from category  where id = $fid";
		$query = $db->query($sql_publish_check);
		$rs_check = $db->fetch_array($query);
		$f_publish = $rs_check["f_publish"];	//parent category's publish state 
	}
}

///************check the category's publish state 	--- leo 2008-07-02 ********************/////


// ¼ûʵҳϢ
// ͨһ $fid=0,$categoryid=һid
// ͨ $fid=һid,$categoryid=id
if (empty($categoryid)) {
    browseTracking(4,$pid,$inventoryid);
} else {
    browseTracking(4,$pid,$inventoryid . '||' . $categoryid);
}


// =============== 1 end ===============================
// =============== 2 start =============================
//delete_flag=1ĲƷʾ
$delete_flag_str = '';

$sql = "SELECT `id` FROM `product` WHERE `delete_flag` = 1";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $delete_flag_ary[] = $rs['id'];   //ΪɾĲƷ
}

if (!empty($delete_flag_ary)) {
    $delete_flag_str = " AND c.`product_id` NOT IN (" . implode(",", $delete_flag_ary) . ") ";
}
// =============== 2 end ===============================

// =============== 3 start =============================
// ֻʾвƷûвƷͲʾ
$have_qty_product_ary = array();
//$sql = "SELECT c.`category_id`, c.`product_id`, i.`qty` FROM `product_inventory` i, `product_category` c WHERE i.`product_id`=c.`product_id` AND c.`seq2`>0 AND i.`qty`>0 AND i.`inventory_id` = '$inventoryid' " . $delete_flag_str . "ORDER BY c.`seq2` ASC";
$sql = "SELECT c.`category_id`, c.`product_id`, i.`qty` FROM `product_inventory` i, `product_category` c WHERE i.`product_id`=c.`product_id` AND i.`qty`>=0 AND i.`inventory_id` = '$inventoryid' " . $delete_flag_str . "ORDER BY c.`seq` ASC";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $category_id_ary[] = $rs['category_id'];   //вƷķ
    $have_qty_product_ary[] = $rs['product_id'];
    $product_qty_ary[$rs['product_id']] = $rs['qty'];
}

$have_qty_product_ary = array_unique($have_qty_product_ary);
$have_qty_product_str = implode(",", $have_qty_product_ary);


$category_id_ary = array_unique($category_id_ary);
$have_set_category_id_str  = $category_id_str = implode(",", $category_id_ary);

$sql = "SELECT `id`, `name`, `pic`, `pic_2`, `seq`, `fid`, `description` FROM `category` WHERE `fid`>0 AND publish=1 AND `id` IN (" . $category_id_str . ")";
$query = $db->query($sql);
$sub_category_ary[] = $categoryid;
while($rs = $db->fetch_array($query)) {
    $category_id_ary[] = $rs['fid'];
    if ($rs['fid'] == $categoryid) {
        $sub_category_ary[] = $rs['id'];
    }

}

$category_id_ary = array_unique($category_id_ary);
$have_set_category_id_str = implode(",", $category_id_ary);

if (!is_numeric($pid) || $pid <= 0) {
    header("Location:product.php");
    exit;
}

// ֻʾп
//$categoryid_str = $categoryid;
if ($fid<=0 && $categoryid<=0) {
    $sql_6 = '';
} else {
    $sql_6 = " AND `fid` IN (0,$cate_id)";
}

$sql = "SELECT `id`, `fid`, `name`, `pic`, `pic_2`, `seq`, `description` FROM `category` WHERE `id` IN (" . $have_set_category_id_str . ") AND publish=1 " . $sql_6 . " ORDER BY `seq` ASC";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    if ($rs['fid'] == 0) {
        $out_ary[] = $rs;
        if ($categoryid == $rs['id']) {
            $category_name = $rs['name'];
            $category_pic_2 = $rs['pic_2'];
        }
    } else {
        $sub_out_ary[] = $rs;
        $categoryid_str .= ',' . $rs['id'];
        if ($categoryid == $rs['id']) {
            $category_name = $rs['name'];
            $category_pic_2 = $rs['pic_2'];
        }
    }
}

$tmp_cate_str = implode(",", $sub_category_ary);
$no_preview_next = false;

if ($gotype == "preview" || $gotype == "next") {
    $pid_ary = array();
    $thisid = false;
    $new_pid = '';

    if ($jansen_type==2) {
        $sql_where = ' WHERE 1 = 1 ';
    } elseif ($jansen_type == 1) {
        $sql_where = ' WHERE `type` = 1 ';
    }


    $sql = "SELECT n.`id`, n.`name`, n.`pic`, n.`type` FROM `inventory` as n, `product_inventory` as i " . $sql_where . $sql_2 . $sql_3 . " GROUP BY n.`id` ORDER BY n.`seq` ASC";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        //$inventory_show_ary[] = $rs;
        if ($inventoryid == $rs['id']) {
            $this_type = $rs['type'];
            //$this_name = $rs['name'];
        }
    }

    if ($this_type == 2) {
        $sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3` FROM `product` p, `product_category` c, `category` ca WHERE ca.id=c.`category_id` AND p.`id`=c.`product_id` AND p.`id` IN (" . $have_qty_product_str . ") GROUP BY p.`id` ORDER BY ca.fid asc, ca.seq asc, p.`code` DESC, p.`id` DESC";
    } else {
        if ($fid<=0 && $categoryid<=0) {
            $sql_4 = '';
        } else {
            $sql_4 = " AND p.`id` IN (" . $have_qty_product_str . ")";
        }

        if ($fid <= 0 && $categoryid <=0) {
            $sql_3 = "";
        } elseif ($fid <= 0) {
            $sql_3 = " AND c.`category_id` IN (" . $tmp_cate_str . ")";
        } else {
            $sql_3 = " AND c.`category_id`='$categoryid'";
        }
        $sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3` FROM `product` p, `product_category` c, `category` ca WHERE ca.`id`=c.`category_id` AND p.`id`=c.`product_id` " . $sql_3 . " " . $sql_4 . " GROUP BY p.`id` ORDER BY ca.`fid` asc, ca.`seq` asc, p.`code` DESC, p.`id` DESC";
    }

    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        if ($new_pid == '') {
            if ($gotype == "next" && $thisid) {
                $new_pid = $rs['id'];
            }
            if ($rs['id'] == $pid) {
                $thisid = true;
                if ($gotype == "preview") {
                    $new_pid = empty($pid_ary) ? $pid : array_pop($pid_ary);
                }
            }
            $pid_ary[] = $rs['id'];
            //echo $new_pid;
        }
    }

    if ($new_pid == "") {
        $new_pid = $pid;
    }
    $pid = $new_pid;


    //echo $pid;exit;
}



//$sql = "SELECT * FROM `product` WHERE `id`='$pid' AND `hidden_flag`=0";
$sql = "SELECT * FROM `product` WHERE `id`='$pid'";
$product_info_ary = $db->get_one($sql);
//echo $sql;
//echo "<pre>";print_r($product_info_ary);exit;
$clearance_flag = false;

$item_flag = '';
if ($product_info_ary['new_flag'] == 1 && $clearance_flag) {
    $item_flag = "<font color='red' size='2'><em>新产品! Clearance!</em></font>";
} elseif ($product_info_ary['new_flag'] == 1) {
    $item_flag = "<font color='red' size='2'><em>新产品!</em></font>";
} elseif ($clearance_flag) {
    $item_flag = "<font color='red' size='2'><em>Clearance!</em></font>";
}


if (trim($product_info_ary['pic']) == "" || !file_exists("./attachments/" . $product_info_ary['pic'])) {
    $product_info_ary['pic'] = 'no_bigCN.gif';
}

$product_info_ary['width'] = sprintf("%0.2f", $product_info_ary['width']);
$product_info_ary['height'] = sprintf("%0.2f", $product_info_ary['height']);
$product_info_ary['length'] = sprintf("%0.2f", $product_info_ary['length']);

$width_inch = sprintf("%0.2f", $product_info_ary['width2']);
$height_inch = sprintf("%0.2f", $product_info_ary['height2']);
$length_inch = sprintf("%0.2f", $product_info_ary['length2']);

$product_info_ary['cbm'] = sprintf("%0.2f", $product_info_ary['cbm']);
$product_info_ary['cuft'] = sprintf("%0.2f", $product_info_ary['cuft']);

$product_info_ary['price_1'] = number_format($product_info_ary['price_1'], 2, '.', ',');
$product_info_ary['price_2'] = number_format($product_info_ary['price_2'], 2, '.', ',');
$product_info_ary['price_3'] = number_format($product_info_ary['price_3'], 2, '.', ',');



$sql = "SELECT `product_id`, `qty`, `special_price`, `special_price2` FROM `product_inventory` WHERE `product_id` ='$pid' AND `inventory_id`='$inventoryid'";
$tmp_ary = $db->get_one($sql);
$sp = $tmp_ary['special_price'];
$sp2 = $tmp_ary['special_price2'];
$qty = $tmp_ary['qty'];
$code = $product_info_ary['code'];


if ($jansen_type==2) {
    $sp = number_format($sp, 2, '.', ',');
    $money_type = '美金';

    if ($sp == 0) {
        $sp_div = '';
    } else {
        $sp = number_format($sp, 2, '.', ',');
        $sp_div = '<div class="price">Special: ' . $sp . ' USD</div>';
        $sp_div = '<tr>
                            <td height="15" colspan=1 align="right" valign="middle"><strong>Special</strong> :</td>                            
                            <td align="right">' . $sp . '</td>
                            <td align="right">&nbsp;&nbsp;' . $money_type . '</td>
                            <td colspan=5 align="right"></td>
                          </tr>
	                   ';

    }
    if ($inventory_type == 2) {
        $price_show = '<tr>
                            <td height="15" colspan=1 align="right" valign="middle"><strong>Regular</strong> :</td>                            
                            <td align="right">' . $product_info_ary['price_2'] . '</td>
                            <td align="left">&nbsp;&nbsp;' . $money_type . '</td>
                            <td colspan=5 align="right"></td>
                          </tr>
	                   ' . $sp_div;
    } else {
        $price_show = '<tr>
                            <td height="15" colspan=1 align="right" valign="middle"><strong>Regular</strong> :</td>                            
                            <td align="right">' . $product_info_ary['price_1'] . '</td>
                            <td align="left">&nbsp;&nbsp;' . $money_type . '</td>
                            <td colspan=5 align="right"></td>
                          </tr>
	                   ' . $sp_div;
    }

} elseif ($jansen_type == 1) {
    $sp = number_format($sp2, 2, '.', ',');
    $money_type = 'HKD';

    if ($sp == 0) {
        $sp_div = '';
    } else {
        $sp = number_format($sp, 2, '.', ',');
        $sp_div = '<tr>
                            <td height="15" colspan=1 align="right" valign="middle"><strong>Special</strong> :</td>                            
                            <td align="right">' . $sp . '</td>
                            <td align="left">&nbsp;&nbsp;' . $money_type . '</td>
                            <td colspan=5 align="right"></td>
                          </tr>
	                   ';

    }

    //$sp = $sp==0 ? ' - ' : (float)$sp . ' (HKD)';
    $price_show = '<tr>
                            <td height="15" colspan=1 align="right" valign="middle"><strong>Price</strong> :</td>                            
                            <td align="right">' . $product_info_ary['price_3'] . '</td>
                            <td align="left">&nbsp;&nbsp;' . $money_type . '</td>
                            <td colspan=5 align="right"></td>
                          </tr>
	                   ' . $sp_div;
}

if($categoryid>0 && $inventoryid > 0) {
    $return_href = "inventory_list.php?inventoryid=$inventoryid&fid=$fid&categoryid=$categoryid";
} else {
    $return_href = "javascript:history.back(1)";
}
$postiondiv='<div class="position" id="position"><a href="index.php">Home</a> &gt; <a href="products.php?categoryid=1001"> Inventory</a> </div>';
$baner_img='<div class="bigtitle">Inventory</div>';

require_once PrintEot('header');
require_once PrintEot('inventory_details');
require_once PrintEot('footer');

?>